// components/common/bind-mobile/index.js
const componentBehavior = require('../../../behaviors/component');
const { COMPONENT_OPTIONS } = require('../../../constants/common');
const { isValidPhone } = require('../../../common/utils');
const { tipToast, tipSuccess } = require('../../../common/env');
const { sendVerifyCode, bindMobile } = require('../../../api/usr');

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        show: {
            type: Boolean,
            value: false,
        },
        title: {
            type: String,
            value: '绑定手机号',
        },
    },
    data: {
        mobile: '',
        code: '',
        _time: 0,
        _isDisable: true,
    },
    observers: {
        'show': function (val) {
            const { _time } = this.data;
            if (_time === 0 || !val) {
                this._reset();
            }
        },
    },
    methods: {
        _reset() {
            this.setData({
                mobile: '',
                code: '',
                _time: 0,
                _isDisable: true,
            });
        },
        handleClose(e) {
            this.setData({
                show: false,
            });
            // this.triggerEvent('close');
            this._reset();
        },
        handleMobileChange(e) {
            const { value } = e.detail;
            this.setData({
                mobile: value,
            });
        },
        handleMobileBlur(e) {
            const { value } = e.detail;
            if (value && !isValidPhone(value)) {
                tipToast('不是正确的手机号');
            }
        },
        handleCodeChange(e) {
            const { value } = e.detail;
            this.setData({
                code: value,
                _isDisable: !isValidPhone(this.data.mobile) || false,
            });
        },
        async handleGetCode() {
            const _ = this;
            const { mobile } = _.data;
            if (!isValidPhone(mobile)) {
                tipToast('请输入正确的手机号');
                return;
            }
            // 发送验证码
            console.log('BindMobile => 发送验证码', mobile);
            let flag = await sendVerifyCode(mobile);
            if (flag) {
                _.setData({
                    _time: 60,
                });
                const _timeInterval = setInterval(function() {
                    const { _time } = _.data;
                    if (_time) {
                        _.setData({
                            _time: _time - 1,
                        });
                    } else {
                        clearInterval(_timeInterval);
                    }
                }, 1000);
            }
        },
        async handleBindMobile() {
            const { mobile, code } = this.data;
            console.log('BindMobile => 执行绑定手机号', mobile, code);
            const info = await bindMobile({ mobile, code });
            if (info) {
                this.triggerEvent('result', info);
                tipSuccess('绑定成功');
                this.handleClose();
            } else {
                this._reset();
            }
        },
    }
})
